define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'express/orderpill/index?batch_id=' + Config.batch_id,
                    add_url: 'express/orderpill/add',
                    edit_url: 'express/orderpill/edit',
                    del_url: 'express/orderpill/del',
                    multi_url: 'express/orderpill/multi',
                    import_url: 'express/orderpill/import',
                    table: 'express_order',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'from_express_no', title: __('来源运单号')},
                        {field: 'user.nickname', title: '下单用户',operate:'LIKE',formatter:function (value,row,index) {
                                return row.user ? "<a href=\"javascript:void(0)\" data-tips-image='"+Fast.api.cdnurl(row.user.avatar)+"'><img class=\"img-sm img-center\" src='"+Fast.api.cdnurl(row.user.avatar)+"'></a>&nbsp;" + value : '';
                            }},
                        {field: 'status', title: __('Status'), searchList: {"created":__('Status created'),"complete":__('Status complete'),"waitingpay":__('Status waitingpay'),"successpay":__('Status successpay'),"delivered":__('Status delivered')}, formatter: Table.api.formatter.status},
                        {field: 'service_ids', title: __('增值服务'), operate:false,formatter:function (value,row,index) {
                                var html = '';
                                for (var i=0;i<row.service.length;i++){
                                    html += row.service[i].name + '    ' + row.service[i].price + '/单<br>';
                                }
                                return html ? html : '无';
                            }},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},

                        {field: 'operate', title: __('Operate'), table: table,buttons: [
                                {
                                    name: 'edit',
                                    text: __('订单详情'),
                                    title: __('订单详情'),
                                    classname: 'btn btn-xs btn-editone btn-primary btn-dialog',
                                    icon: 'fa fa-list',
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        return true;
                                    }
                                },


                            ], events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            $(document).on("click", ".btn-addBatch", function () {
                var that = this;
                var id = "addBatchTpl";
                var content = Template(id, {});
                Layer.open({
                    type: 1,
                    title: "加入批次",
                    area: ["500px", "300px"],
                    content: content,
                    success: function (layero) {
                        var form = $("form", layero);
                        var ids = Table.api.selectedids(table);
                        $('#c-ids').val(ids.join(','));
                        Form.api.bindevent(form, function (data) {
                            $(".btn-refresh").trigger("click");
                            Layer.closeAll();
                        });
                    }
                });
            });
            $(document).on("click", "input[name='row[type]']", function () {
                var type = $(this).val();
                if(type == 'old'){
                    $('.batch-select').removeClass('hide')
                }else{
                    $('.batch-select').addClass('hide')
                }
            });
            $(document).on("click", ".btn-invoice", function () {
                if($(this).data('id')){
                    Backend.api.open('express/order/print/ids/' + $(this).data('id'), __('打印发货单'),{area:['80%', '80%']});
                }else{
                    Backend.api.open('express/order/print/ids/' + Table.api.selectedids(table), __('打印发货单'),{area:['80%', '80%']});
                }
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});